final$tert_pvi <- as.numeric(cut(final$pvi_state, quantile(final$pvi_state, c(0, 1/3, 2/3, 1)), include.lowest = TRUE))

final$tert_pvi <- factor(final$tert_pvi, labels = c('Low PVI', 'Medium PVI', 'High PVI'))

tt1 <- ggplot()+
  geom_smooth(data = final %>% filter(days < 1), aes(days, violence_range, color = tert_pvi, fill = tert_pvi, linetype = tert_pvi), method = 'loess', span =.9)+
  geom_smooth(data = final %>% filter(days > -1), aes(days, violence_range, color = tert_pvi, fill = tert_pvi, linetype = tert_pvi), method = 'loess', span =.5)+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(0,0.4))+
  scale_x_continuous(breaks = seq(-60,340,20))+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  labs(fill = "Competitiveness", linetype = 'Competitiveness', color = 'Competitiveness')+
  xlab('Days before/after 2022 Election')+
  ylab('Support Political Violence')+
  theme(legend.position = 'none',
        axis.text = element_text(size = 8),
        axis.title = element_text(size = 9))

tt2 <- ggplot()+
  geom_smooth(data = final %>% filter(days < 1), aes(days, norms_range, color = tert_pvi, fill = tert_pvi, linetype = tert_pvi), method = 'loess', span =.9)+
  geom_smooth(data = final %>% filter(days > -1), aes(days, norms_range, color = tert_pvi, fill = tert_pvi, linetype = tert_pvi), method = 'loess', span =.5)+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(0.2,0.6))+
  scale_x_continuous(breaks = seq(-60,340,20))+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  labs(fill = "Competitiveness", linetype = 'Competitiveness', color = 'Competitiveness')+
  xlab('Days before/after 2022 Election')+
  ylab('Support Norm Violations')+
  theme(legend.position = 'none',
        axis.text = element_text(size = 8),
        axis.title = element_text(size = 9))

tt3 <- ggplot()+
  geom_smooth(data = final %>% filter(days < 1), aes(days, affpol, color = tert_pvi, fill = tert_pvi, linetype = tert_pvi), method = 'loess', span =.9)+
  geom_smooth(data = final %>% filter(days > -1), aes(days, affpol, color = tert_pvi, fill = tert_pvi, linetype = tert_pvi), method = 'loess', span =.5)+
  theme_bw()+
  geom_vline(xintercept = 0, color = 'red', linetype=2)+
  coord_cartesian(ylim = c(20,80))+
  scale_x_continuous(breaks = seq(-60,340,20))+
  scale_color_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  scale_fill_manual(values=c("#cc6677", "#E69F00", "#56B4E9"))+
  labs(fill = "Competitiveness", linetype = 'Competitiveness', color = 'Competitiveness')+
  xlab('Days before/after 2022 Election')+
  ylab('Affective Polarization')+
  theme(legend.position = 'bottom',
        axis.text = element_text(size = 8),
        axis.title = element_text(size = 9),
        legend.text=element_text(size=8),
        legend.title=element_text(size=9))

final_figure_pvi <- suppressMessages(cowplot::plot_grid(tt1,tt2,tt3, labels = c('A', 'B', 'C'),
                                       label_size = 9,
                                       align = "v", nrow = 3, rel_widths = c(1, 1, 1), rel_heights = c(1, 1, 1.2)))

suppressMessages(print(final_figure_pvi))
#ggsave(final_figure_pvi, file = 'fig4_final.png', units = 'in', height = 7.8, width = 7.25)